﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AdminTool_Default : System.Web.UI.Page
{
    string title = "";
    string stringDate = "";
    string author = "";
    string content = "";
    string summary = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        txtDate.Text = DateTime.Now.ToShortDateString();

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        DateTime dateTime;
        bool flagdate = DateTime.TryParse(stringDate, out dateTime);
        if(flagdate)
        {
            title = txtTile.Text;
            author = txtAuthor.Text;
            content = FCKeditor1.Value;
            summary = txtSummary.Text;

            ShoppingDataContext spdt = new ShoppingDataContext();
            var member = from p in spdt.NEWs
                         select p;
            int temp = 1;
            int flag1 = 0;
            int flag = 1;
            int ProId = 0;
            string tam = "N";
            while (flag != 0)
            {
                foreach (var mem in member)
                {
                    string id = mem.NewsID.Substring(2, 4);
                    ProId = int.Parse(id);
                    if (ProId == temp)
                    {
                        temp++;
                        flag1 = 0;
                        break;
                    }

                    flag1 = 1;
                }
                if (flag1 == 1)
                {
                    flag = 0;
                    ProId = temp;
                }

            }
            for (int i = 1; i <= 4 - ProId.ToString().Length; i++)
            {
                tam = tam + "0";
            }
            tam = tam + ProId.ToString();

            
            NEW news = new NEW();
            news.NewsID = tam;
            news.title = title;
            news.writer = Session["user"].ToString();
            news.newscontent = content;
            news.summary = content.Substring(0, 150);
            spdt.NEWs.InsertOnSubmit(news);
        }
        else
        {
            Label1.Text = "ngày thàng sai";
        }
    }
}
